import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

// key-value 模型
public class HashBucket {
    private static class Node {
        private int key;
        private int value;
        Node next;
        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }
    private Node[]  array;
    private int size;   // 当前的数据个数
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 10;//默认桶的大小
    public HashBucket() {
        array = new Node[DEFAULT_SIZE];
    }

    public boolean putLast(int key, int value) {
        int index = key % array.length;//存放下标
        Node cur = array[index];//得到头节点
        while(cur != null){
            if(cur.key == key){
                cur.value = value;
                return true;
            }
            cur = cur.next;
        }

        cur = array[index];
        Node node = new Node(key,value);//新建节点
        if(array[index] == null){
            array[index] = node;
            size++;
            if(loadFactor() > 0.75){
                resize();
            }
            return true;
        }
        while (cur.next != null){//找到尾节点
            cur = cur.next;
        }
        cur.next = node;//尾插
        size++;
        if(loadFactor() > 0.75){
            resize();
        }
        return true;
    }

    public boolean putFirst(int key, int value) {
        int index = key % array.length;//存放下标
        Node cur = array[index];//得到头节点
        while(cur != null){
            if(cur.key == key){
                cur.value = value;
                return true;
            }
            cur = cur.next;
        }
        Node node = new Node(key,value);//新建节点
        node.next = array[index];//头插法
        array[index] = node;
        size++;
        if(loadFactor() > 0.75){
            resize();
        }
        return true;
    }

    /**
     * 扩容时要将所有的元素重新哈希！！！
     */
    private void resize() {
        Node[] tmpArr = new Node[array.length*2];
        for (int i = 0; i < array.length; i++) {
            Node cur = array[i];
            while(cur != null){
                Node curNext = cur.next;//记录cur.next
                int newIndex = cur.key % tmpArr.length;//找到新数组的下标
                cur.next = tmpArr[newIndex];//进行头插
                tmpArr[newIndex] = cur;
                cur = curNext;//cur向后移动
            }
        }
        array = tmpArr;//将新的数组赋给array
    }

    private double loadFactor() {
        return size * 1.0 / array.length;
    }


    public int get(int key) {
        int index = key % array.length;
        Node cur = array[index];
        while(cur != null){
            if(cur.key == key){
                return cur.value;
            }
            cur = cur.next;
        }
        System.out.println("没有该值");
        return -1;
    }

    public static List<String> splitWordsBySeparator(List<String> words, char separator) {
        List<String> ans = new LinkedList<>();
        System.out.println(words.get(0));
        String s = words.get(0);
        System.out.println(s);
        String[] tmp = s.split("\\."+"");

        return ans;
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("one.two.three");
        splitWordsBySeparator(list,'.');
    }
}
